import { parse } from '@vue/compiler-dom'

function parseTemplateForInteliCaptures(templateString) {
  const ast = parse(templateString)
  const result = {
    node: null,
    conditions: []
  }

  function traverse(node) {
    if (node.type === 1) { // 元素节点
      // 查找包含inteliCaptures的label指令
      const labelDirective = node.props.find(
        prop => 
          prop.type === 7 && // Directive类型
          prop.name === 'bind' && // v-bind指令
          prop.arg?.content === 'label' && // 绑定到label属性
          prop.exp?.content.includes('') // 表达式内容包含目标字符串
      )
      
      if (labelDirective) {
        // 查找v-if或v-show指令
        const conditionDirective = node.props.find(
          prop => 
            prop.type === 7 && 
            (prop.name === 'if' || prop.name === 'show')
        )
        
        if (conditionDirective) {
          result.node = node
          result.conditions = splitLogicalExpression(conditionDirective.exp.content)
        }
        return true
      }
      
      // 继续遍历子节点
      if (node.children) {
        for (const child of node.children) {
          if (traverse(child)) return true
        }
      }
    }
    return false
  }

  traverse(ast.children[0])
  return result
}

// 拆分逻辑表达式的辅助函数
function splitLogicalExpression(expression) {
  // 移除首尾空格
  expression = expression.trim()
  
  // 如果表达式不包含逻辑运算符，直接返回
  if (!expression.includes('&&') && !expression.includes('||')) {
    return [expression]
  }
  
  const conditions = []
  let current = ''
  let parenCount = 0
  
  for (let i = 0; i < expression.length; i++) {
    const char = expression[i]
    
    if (char === '(') parenCount++
    if (char === ')') parenCount--
    
    // 检查是否是逻辑运算符且不在括号内
    if (parenCount === 0) {
      if (expression.slice(i, i + 2) === '&&') {
        conditions.push(current.trim())
        current = ''
        i += 1 // 跳过第二个&
        continue
      }
      
      if (expression.slice(i, i + 2) === '||') {
        conditions.push(current.trim())
        current = ''
        i += 1 // 跳过第二个|
        continue
      }
    }
    
    current += char
  }
  
  // 添加最后一个条件
  if (current.trim()) {
    conditions.push(current.trim())
  }
  
  return conditions
}

// 使用示例
const templateString = `<template>
  <el-form
    :model="oParams"
    :rules="rules"
    ref="em_form"
    label-width="252px"
    content-width="480px"
  >
    <!-- 图片附件 & 抓图时间间隔 -->
    <div v-if="oCapbilities.bSptSnapShoot">
      <el-form-item :label="$t('attachedImage')">
        <el-switch v-model="oParams.bEnablePic"></el-switch>
      </el-form-item>
      <el-form-item :label="$t('captureInterval')" v-show="oParams.bEnablePic">
        <el-select v-model="oParams.szPicInterval" class="el-input--width">
          <el-option v-for="(item) in oCapbilities.aEmailOptions" :key="item" :value="item" :label="item + $t('seconds')"></el-option>
        </el-select>
      </el-form-item>
      <!-- 后智能周界抓图 -->
      <el-form-item v-if="oCapbilities.bSupportInteliCaptureEnabled && oParams.bEnablePic" :label="$t('inteliCaptures')">
        <el-switch v-model="oParams.inteliCaptureEnabled"></el-switch>
      </el-form-item>
    </div>
  </el-form>
</template>`

const result = parseTemplateForInteliCaptures(templateString)
console.log('条件表达式:', result.conditions) 
// 应输出: ['oCapbilities.bSupportInteliCaptureEnabled', 'oParams.bEnablePic']